<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class order_model extends CI_Model
{
	
	public function __construct()
	{
		parent::__construct();

	}
public function insert_order($insert)
{
$this->db->insert('nonwarranty_order',$insert);
return  $this->db->insert_id();
}	
public function get_view()
	{
	$this->datatables
			->select("nonwarranty_order.id as id,nonwarranty_case.case_no,nonwarranty_order.order_no,customer.customer_name,nonwarranty_order.order_date,part_supplier.name,nonwarranty_order.expected_date")
			->from('nonwarranty_order','nonwarranty_case','customer','part_supplier')	
            ->join('nonwarranty_case','nonwarranty_case.id=nonwarranty_order.case_id')
            ->join('customer','customer.service_id=nonwarranty_case.service_id')	
            ->join('part_supplier'	,'part_supplier.id=nonwarranty_order.id_supplier')	
			->add_column("Actions",
			"<center>			</center>", "id")
			
			;
	   return  $this->datatables->generate();
	}

public function get_case($id)
	{
	$this->db->select('nonwarranty_case.case_no,part_supplier.name,nonwarranty_case.id,nonwarranty_order.id as order_id');
	$this->db->from('nonwarranty_case', 'nonwarranty_order','part_supplier' );
	$this->db->join('nonwarranty_order', 'nonwarranty_case.id= nonwarranty_order.case_id '  );
	$this->db->join('part_supplier','nonwarranty_order.id_supplier=part_supplier.id');
	$this->db->where('nonwarranty_order.order_no',$id);
	$q = $this->db->get();
	return $q;
		
	}
public function get_supplier()
{
$query= $this->db->get('part_supplier');
	return $query->result();

}
public function update_nonwarranty_part($id,$data)
{
$insert=array('nonwarranty_order_id'=>$data,);
$this->db->update('nonwarranty_part',$insert,array('case_id'=>$id));
}
public function get_supplier_id($name)
{
$query = $this->db->get_where('part_supplier', array('name' => $name));
		return $query;

}

public function insert_received_order($insert)
{
$this->db->insert('received_order',$insert);
return  $this->db->insert_id();
}
public function insert_received_part($insert)
{
$this->db->insert('received_order_part',$insert);
}
public function update_nonwarranty_case($id)
{
$insert=array('status_id'=>4,);
$this->db->update('nonwarranty_case',$insert,array('id'=>$id));
}
public function update_case_status($id)
{
$insert=array('status_id'=>5,);
$this->db->update('nonwarranty_case',$insert,array('id'=>$id));
}
public function part_detail($id)
{
$query = $this->db->get_where('nonwarranty_part', array('case_id' => $id));
		return $query->result();
}
public function received_view()
{
	$this->datatables
			->select("received_order.id as id,received_order.case_no,nonwarranty_order.order_no,part_supplier.name as supplier,nonwarranty_order.order_date,received_order.received_date,status_nonwarranty.name,customer.customer_name ")
			->from('received_order','nonwarranty_order','nonwarranty_case','customer','part_supplier','status_nonwarranty')	
            ->join('nonwarranty_order','nonwarranty_order.id=received_order.nonwarranty_order_id')
			 ->join('nonwarranty_case','nonwarranty_case.id=received_order.case_id')
			 ->join('status_nonwarranty','nonwarranty_case.status_id=status_nonwarranty.id')
            ->join('customer','customer.service_id=nonwarranty_case.service_id')	
            ->join('part_supplier'	,'part_supplier.id=nonwarranty_order.id_supplier')	
			->add_column("Actions",
			"<center>			
			<a href='index.php/admin/order/assigned_engineer/$1'>
								<i class='icon-edit '></i>
								</a>
			
			</center>", "id")
			
			;
	   return  $this->datatables->generate();
	}
public function notassigned_view()
{
	$this->datatables
			->select("received_order.id as id,received_order.case_no,nonwarranty_order.order_no,part_supplier.name as supplier,nonwarranty_order.order_date,received_order.received_date,status_nonwarranty.name,customer.customer_name ")
			->from('received_order','nonwarranty_order','nonwarranty_case','customer','part_supplier','status_nonwarranty')	
            ->join('nonwarranty_order','nonwarranty_order.id=received_order.nonwarranty_order_id')
			 ->join('nonwarranty_case','nonwarranty_case.id=received_order.case_id')
			 ->join('status_nonwarranty','nonwarranty_case.status_id=status_nonwarranty.id')
            ->join('customer','customer.service_id=nonwarranty_case.service_id')	
            ->join('part_supplier'	,'part_supplier.id=nonwarranty_order.id_supplier')	
			->where('nonwarranty_case.status_id',5)
			->add_column("Actions",
			"<center>			
			<a href='index.php/admin/order/assigned_engineer/$1'>
								<i class='icon-edit '></i>
								</a>
			
			</center>", "id")
			
			;
	   return  $this->datatables->generate();
	}

public function get_engineer()
	{
	$this->db->select('users.*');
	$this->db->from('users', 'users_groups' );
	$this->db->join('users_groups', 'users.id= users_groups.user_id   '  );
	$this->db->where('users_groups.group_id',2);
	$q = $this->db->get();
		if($q->num_rows() > 0) {
			foreach (($q->result()) as $row) {
				$data[] = $row;
			}
			return $data;
			}
	}	
public function detail_order($id)
{
$this->db->select('received_order.case_id,received_order.case_no,nonwarranty_order.order_no');
	$this->db->from('received_order', 'nonwarranty_order' );
	$this->db->join('nonwarranty_order', 'nonwarranty_order.id= received_order.nonwarranty_order_id   '  );
	$this->db->where('received_order.id',$id);
	$q = $this->db->get();
	return $q;

}
public function engineer_assigned($insert)
{
$this->db->insert('nonwarranty_repair',$insert);
}
public function update_case($id)
{
$insert=array('status_id'=>6,);
$this->db->update('nonwarranty_case',$insert,array('id'=>$id));

}
public function delete_diagnosis($id)
{
$this->db->delete('nonwarranty_diagnosis',array('case_id'=>$id));		

}
	
}	
?>